<template>
    <div class="login">
        <el-form ref="pwdRef" :model="user" :rules="rules" class="login-form">
            <h3 class="title">后台管理系统</h3>
            <el-form-item prop="username">
                <el-input v-model="user.username" type="text" size="large" auto-complete="off"
                    placeholder="账号">
                    <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
                </el-input>
            </el-form-item>
            <el-form-item prop="email">
                <el-input v-model="user.email" placeholder="请输入邮箱" type="text" />
            </el-form-item>
            <el-form-item prop="newPassword" >
                <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password />
            </el-form-item>
            <el-form-item prop="confirmPassword" >
                <el-input v-model="user.confirmPassword" placeholder="请确认新密码" type="password" show-password />
            </el-form-item>
            <el-form-item prop="authCode">
                <el-input v-model="user.authCode" type="authCode" size="large" auto-complete="off" placeholder="验证码"
                    >
                    <template #prefix><svg-icon icon-class="code" class="el-input__icon input-icon" /></template>
                </el-input>
                <el-button v-if=!authOpen @click="sendEmailCode">发送验证码</el-button>
            </el-form-item>
            <el-form-item style="width:100%;">
                <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click="submit">
                    <span v-if="!loading">提交</span>
                    <span v-else>提交中...</span>
                </el-button>
                <div style="float: right;">
                    <router-link class="link-type" :to="'/login'">返回登录</router-link>
                </div>
            </el-form-item>
        </el-form>
        <!--  底部  -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
        </div>
    </div>
</template>

<script setup>
import { findUserPwd } from "@/api/system/user";
import { getEmailCode } from "@/api/login";

const { proxy } = getCurrentInstance();
const loading = ref(false);
const router=useRouter();

const user = reactive({
    username: null,
    email:null,
    newPassword:null,
    confirmPassword:null,
    authCode:null
});
function sendEmailCode(){
    getEmailCode(user.username,user.email).then(response=>{
        console.log(response.data);
    })
}
const equalToPassword = (rule, value, callback) => {
    if (user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
    } else {
        callback();
    }
};

const rules = ref({
    newPassword: [{ required: true, message: "新密码不能为空", trigger: "blur" }, { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }, { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }],
    confirmPassword: [{ required: true, message: "确认密码不能为空", trigger: "blur" }, { required: true, validator: equalToPassword, trigger: "blur" }],
    // authCode: [{ required: true, trigger: "change", message: "请输入验证码" }]
});

/** 提交按钮 */
function submit() {
    proxy.$refs.pwdRef.validate(valid => {
        if (valid) {
            findUserPwd(user.username, user.email, user.confirmPassword, user.authCode).then(response => {
                proxy.$modal.msgSuccess("修改成功");
                router.push("/login")
            }).catch(error=>{
                proxy.$modal.msgError(error.message);
            });
        }
    });
};

</script>

<style lang='scss' scoped>
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/技师2.jpg");
    background-size: cover;
}

.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}

.login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
        height: 40px;

        input {
            height: 40px;
        }
    }

    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 0px;
    }
}

.login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}

.login-code {
    width: 33%;
    height: 40px;
    float: right;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}

.login-code-img {
    height: 40px;
    padding-left: 12px;
}
</style>
